﻿@{
    ViewData["Title"] = "任务调度管理";
}
<style>
    .layui-form-label { width: 120px; }
    .layui-input-block { margin-left: 150px }</style>
<div class="layui-layout layui-layout-admin">

    <form class="layui-form" action="" lay-filter="val-filter">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">触发器编号</label>
                <div class="layui-input-inline">
                    <input type="hidden" name="id" value="0">
                    <input type="hidden" name="jobId" value="">
                    <input type="text" name="triggerId" autocomplete="off" lay-verify="required" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">触发器类型</label>
                <div class="layui-input-inline">
                    <input type="hidden" name="args" id="args" />
                    <select name="triggerType" lay-filter="triggerType">
                        <option value="Furion.Schedule.PeriodTrigger">间隔</option>
                        <option value="Furion.Schedule.CronTrigger">Cron表达式</option>
                    </select>
                </div>
            </div>
        </div>

        <div class="layui-form-item" id="showInterval">
            <label class="layui-form-label">间隔时间(ms)</label>
            <div class="layui-input-inline">
                <input type="number" id="interval" lay-affix="number" step="1000" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item" id="showCron">
            <label class="layui-form-label">Cron表达式</label>
            <div class="layui-input-block">
                <input type="text" id="cron" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">起始时间</label>
                <div class="layui-input-inline">
                    <input type="text" name="startTime" id="startTime" autocomplete="off" lay-verify="date" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">结束时间</label>
                <div class="layui-input-inline">
                    <input type="text" name="endTime" id="endTime" autocomplete="off" lay-verify="date" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">最大触发次数</label>
                <div class="layui-input-inline">
                    <input type="number" value="0" lay-affix="number" name="maxNumberOfRuns" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">最大出错次数</label>
                <div class="layui-input-inline">
                    <input type="number" value="0" lay-affix="number" name="maxNumberOfErrors" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">重试次数</label>
                <div class="layui-input-inline">
                    <input type="number" value="0" lay-affix="number" name="numRetries" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">重试间隔(ms)</label>
                <div class="layui-input-inline">
                    <input type="number" value="10000" lay-affix="number" step="1000" name="retryTimeout" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">立即启动</label>
                <div class="layui-input-inline">
                    <input type="radio" name="startNow" value="true" title="是" checked>
                    <input type="radio" name="startNow" value="false" title="否">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">启动时执行一次</label>
                <div class="layui-input-inline">
                    <input type="radio" name="runOnStart" value="true" title="是" checked>
                    <input type="radio" name="runOnStart" value="false" title="否">
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">重置触发次数</label>
            <div class="layui-input-block">
                <input type="radio" name="resetOnlyOnce" value="true" title="是" checked>
                <input type="radio" name="resetOnlyOnce" value="false" title="否">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">描述信息</label>
            <div class="layui-input-block">
                <textarea name="description" class="layui-textarea"></textarea>
            </div>
        </div>
    </form>

</div>

@section scripts {
    <script type="text/javascript">
        var app = {};
        layui.use(['http', 'toast', 'cron'], function () {
            var form = layui.form, $ = layui.$, laydate = layui.laydate,
                http = layui.http, toast = layui.toast, cron = layui.cron;
            
            app.triggerType = function (type) {
                var value = String(app.data.args||[]);
                if (type == 'Furion.Schedule.CronTrigger') {
                    $('#showInterval').hide();
                    $('#showCron').show();

                    var cron = '';                    
                    var parameters = value.split(',');
                    if (parameters.length == 2) {
                        cron = parameters[0].replace(new RegExp('"', 'gm'), '').trim();
                    } else if (parameters.length > 2) {
                        const temp = value.substring(0, value.lastIndexOf(','));
                        cron = temp.replace(new RegExp('"', 'gm'), '').trim();
                    }
                    $('#cron').val(cron)
                }
                else {
                    $('#showInterval').show();
                    $('#showCron').hide();
                    if (value == 'undefined') value = '5000';
                    $('#interval').val(value)
                }
            }
            app.save = function () {
                form.submit('val-filter', function (data) {
                    var postData = data.field
                    //
                    if (postData.triggerType == 'Furion.Schedule.CronTrigger') {
                        var newValue = $('#cron').val()
                        var cronStringFormatValue = -1;
                        // 如果是 Macro 标识符，使用默认格式
                        if (newValue.startsWith('@@'))
                            cronStringFormatValue = 0; // 默认格式，书写顺序：分 时 天 月 周
                        else {
                            if (newValue.split(' ').length == 6)
                                cronStringFormatValue = 2; // 带秒格式，书写顺序：秒 分 时 天 月 周
                            else
                                cronStringFormatValue = 3; // 带秒和年格式，书写顺序：秒 分 时 天 月 周 年
                        }
                        postData.args = `"${newValue}",${cronStringFormatValue}`;
                    }
                    else {
                        postData.args = $('#interval').val()
                    }
                    //
                    var url = postData.id == '0' ? '/api/sysJob/addJobTrigger' : '/api/sysJob/UpdateJobTrigger'
                    http.post(url, postData).then((res) => {
                        if (res.code == 200) {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            parent.layui.table.reload('lsttable');
                            toast.success({ message: '保存成功！' });
                        }
                        else {
                            layer.alert('保存失败，错误信息:' + res.message, { icon: 2 });
                        }
                    });
                    return false;
                });
            }
            //
            app.initData = function (data) {
                form.val('val-filter', data);
                laydate.render({ elem: '#startTime' });
                laydate.render({ elem: '#endTime' });
                cron.render({ elem: "#cron" });

                //
                app.triggerType(data.triggerType);
                form.on('select(triggerType)', function (data) {
                    app.triggerType(data.value)
                });
                
                //$('input[name=startNow][value=' + data.startNow + ']').prop('checked', true);
                //$('input[name=runOnStart][value=' + data.runOnStart + ']').prop('checked', true);
                //$('input[name=resetOnlyOnce][value=' + data.resetOnlyOnce + ']').prop('checked', true);
                form.render('radio');
            }();
        })
    </script>
}
